#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll n;
string s;

void solve()
{
    cin >> n >> s;
    ll len0 = 0, len1 = 0, t0 = 0, t1 = 0;
    ll cnt0 = 0, cnt1 = 0;
    for (ll i = 0;i < n;i++) {
        if (s[i] == '0') {
            cnt0++;
            t0++;
            len1 = max(len1, t1);
            t1 = 0;
        } else {
            cnt1++;
            t1++;
            len0 = max(len0, t0);
            t0 = 0;
        }
    }
    len1 = max(len1, t1);
    len0 = max(len0, t0);

    cout << min(cnt1 + 2 * cnt0 - 2 * len0, cnt0 + 2 * cnt1 - 2 * len1) << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    cin >> T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
    }